//
// Created by webturing on 2024/1/1.
//
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 105;
int cas, ok[N][N];//cas测试编号,ok表示两者是否可达
bool vis[N][N];//表示是否已经出现过
void Floyd() {
    for (int k = 1; k <= 26; k++)
        for (int i = 1; i <= 26; i++)
            for (int j = 1; j <= 26; j++)
                ok[i][j] = min(ok[i][j], ok[i][k] + ok[k][j]);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        memset(ok, 0x3f, sizeof ok);//多组初始化
        memset(vis, false, sizeof vis);
        int n;
        scanf("%d", &n);
        while (n--) {
            char str[5];
            scanf("%s", str);
            int x = str[0] - 'A' + 1, y = str[2] - 'A' + 1;
            if (str[1] == '<')//小于
            {
                ok[x][y] = 1;
                vis[x][y] = true;
            } else//反一下
            {
                ok[y][x] = 1;
                vis[y][x] = true;
            }
        }
        Floyd();
        bool flag = false;
        printf("Case %d:\n", ++cas);
        for (int i = 1; i <= 26; i++) {
            for (int j = 1; j <= 26; j++)//需要从1开始
            {
                if (ok[i][j] != 0x3f3f3f3f && !vis[i][j])//两者可达且没有被输出过
                {
                    printf("%c<%c\n", i + 'A' - 1, j + 'A' - 1);
                    vis[i][j] = true;
                    flag = true;
                }
            }
        }
        if (!flag) printf("NONE\n");
    }
    return 0;
}

